package com.lsh.oms.core.proxy.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.oms.core.utils.http.HttpClientTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author panxudong
 * @date 16/12/20
 */
@Component
@Slf4j
public class DmallProxy extends BaseProxy {

    @Value("${dmallUrl}")
    private String dmallUrl;

    @Value("${dmall.secretKey}")
    private String secretKey;

    /**
     * 推送多点订单完成
     * @param req
     * @return
     */
    public boolean pushDmall(JSONObject req) {
        Map<String, String> head = new HashMap<>(8);
        head.put("interfaceCode", "fulfillment-waybill-DeliveryCoreService-maxOrderComplete");
        head.put("uid", "wumartMax");
        head.put("secretKey", secretKey);
        head.put("Content-Type", "application/json");

        log.info("dmall push param is " + req.toJSONString());
        String res = HttpClientTemplate.doPostBody(dmallUrl, req.toJSONString(), head);

        if (StringUtils.isEmpty(res)) {
            return false;
        }

        JSONObject dmall = JSON.parseObject(res);
        if (null == dmall) {
            return false;
        }
        String code = dmall.getString("code");
        if (StringUtils.isEmpty(code) || !code.endsWith("0000")) {
            String message = dmall.getString("message");
            if(!StringUtils.isEmpty(message) && message.contains("订单妥投")){
                return true;
            }
            return false;
        }

        return true;
    }

    /**
     * 推送多点
     *
     * @param req
     * @return
     */
    public boolean pushShipDmall(JSONObject req) {
        Map<String, String> head = new HashMap<>(8);
        head.put("interfaceCode", "fulfillment-waybill-DeliveryStationService-maxOrderAccept");
        head.put("uid", "wumartMax");
        head.put("secretKey", secretKey);
        head.put("Content-Type", "application/json");

        log.info("dmall push ship time param is " + req.toJSONString());
        String res = HttpClientTemplate.doPostBody(dmallUrl, req.toJSONString(), head);

        if (StringUtils.isEmpty(res)) {
            return false;
        }

        JSONObject dmall = JSON.parseObject(res);
        String code = dmall.getString("code");
        if (StringUtils.isEmpty(code) || !code.endsWith("0000")) {
            String message = dmall.getString("message");
            if(!StringUtils.isEmpty(message) && message.contains("订单妥投")){
                return true;
            }

            return false;
        }

        return true;
    }

}
